package com.zhs.gccs.view.taskMap;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.annimon.stream.Stream;
import com.zhs.gccs.R;
import com.zhs.gccs.dao.TaskGccjFileDao;
import com.zhs.gccs.model.AcFile;
import com.zhs.gccs.model.AcFileTemp;
import com.zhs.gccs.model.CaData;
import com.zhs.gccs.model.CaDict;
import com.zhs.gccs.model.CaDictDataVo;
import com.zhs.gccs.model.CaFile;
import com.zhs.gccs.model.CjFile;
import com.zhs.gccs.model.Project;
import com.zhs.gccs.model.SmTask;
import com.zhs.gccs.model.StatisticsDict;
import com.zhs.gccs.model.TaskProject;
import com.zhs.gccs.view.BaseView;
import com.zhs.gccs.view.LocationAddress;

import java.util.List;

//工程措施抽检
public class ProjectGccsDataFormDetailView extends BaseView {

    private GccjTakeImageView take_image;
    private GccjTakeImageView take_video;
    private TextView tv_project;//项目名称
    private TextView et_input_val;//输入值
    private TextView tv_location;//位置
    private TextView et_input_remarks;//备注
    private RadioButton rb_fh;//抽检结果符合
    private RadioButton rb_bfh;//抽检结果不符合
    private String cjjgsffh = "1";//符合1(默认) 不符合0

    private RadioButton rb_fhtz;// 符合通则
    private RadioButton rb_bfhtz;//抽检结果不符合通则
    private String cjjgsffhtz = "1";//符合1(默认) 不符合0

    ///////////////////////////////
    private Activity activity;
    TaskProject taskProject;
    SmTask smTask;
    StatisticsDict caType;
    StatisticsDict currentLevel;
    LocationAddress location;
    private List<CaDict> dictsOfForm;
    private CaData data;//表单数据
    ///////////////////////////////////////

//    private TextView tv_required;//是否必填
    private TextView tv_title;//抽检项标题
    private TextView tv_cjsl;//抽检数量要求
    private TextView tv_cjff;//检测方法/工具
    private TextView tv_cjpjbz;//评价标准
    private TaskGccjFileDao taskGccjFileDao;
    public ProjectGccsDataFormDetailView(@NonNull Context context) {
        super(context);
    }

    public ProjectGccsDataFormDetailView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ProjectGccsDataFormDetailView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_view_project_gccs_data_detail, this, true);
//        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomerLogo);
//        String title = typedArray.getString(R.styleable.CustomerInfoItem_preText);
//        typedArray.recycle();
        taskGccjFileDao = new TaskGccjFileDao();
        take_image = view.findViewById(R.id.take_image);
        take_video = view.findViewById(R.id.take_video);
        tv_project = view.findViewById(R.id.tv_project);
        et_input_val = view.findViewById(R.id.et_input_val);
        tv_location = view.findViewById(R.id.tv_location);
        et_input_remarks = view.findViewById(R.id.et_input_remarks);
        rb_fh = view.findViewById(R.id.rb_fh);
        rb_bfh = view.findViewById(R.id.rb_bfh);
        cjjgsffh = "1";
        rb_fh.setChecked(true);
        rb_fh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)cjjgsffh = "1";
            }
        });
        rb_bfh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)cjjgsffh = "0";
            }
        });

        rb_fhtz = view.findViewById(R.id.rb_fhtz);
        rb_bfhtz = view.findViewById(R.id.rb_bfhtz);
        cjjgsffhtz = "1";
        rb_fhtz.setChecked(true);
        rb_fhtz.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)cjjgsffhtz = "1";
            }
        });
        rb_bfhtz.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)cjjgsffhtz = "0";
            }
        });

        tv_title = view.findViewById(R.id.tv_title);
        tv_cjsl = view.findViewById(R.id.tv_cjsl);
        tv_cjff = view.findViewById(R.id.tv_cjff);
        tv_cjpjbz= view.findViewById(R.id.tv_cjpjbz);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
//        if (EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().unregister(this);
//        }
    }

    public void setLocation(LocationAddress location) {
        if(location==null)return;
        this.location = location;
        //如果是新增
        tv_location.setText(location.getAddress());
    }



    public interface DataFormListener{
        void onCancel();

        void onSave(TaskProject taskProject, SmTask smTask, StatisticsDict caType,
                    StatisticsDict currentLevel,
                    CaDictDataVo dictDataVo, List<CaFile> photos);
    }
    private DataFormListener dataFormListener;

    public void setDataFormListener(DataFormListener dataFormListener) {
        this.dataFormListener = dataFormListener;
    }

    public void loadDataView(Activity activity,
                             SmTask smTask,
                             Project smParticulars,
                             CaDict cict,
                             List<CaDict> dicts,
                             CaData data, List<AcFile> files) {
        this.data = data;
        this.dictsOfForm = dicts;
        //页面初始化规则
        if(dictsOfForm!=null){
            CaDict cjslDict = getDictByCode(CaDict.key_cjsl);
            if(cjslDict!=null&& !TextUtils.isEmpty(cjslDict.getDictValue())){
                tv_cjsl.setText(cjslDict.getDictValue());
            }else {
                tv_cjsl.setText("");
            }
            CaDict cjffDict = getDictByCode(CaDict.key_cjff);
            if(cjffDict!=null&& !TextUtils.isEmpty(cjffDict.getDictValue())){
                tv_cjff.setText(cjffDict.getDictValue());
            }else {
                tv_cjff.setText("");
            }
            CaDict cjpjbzDict = getDictByCode(CaDict.key_cjpjbz);
            if(cjpjbzDict!=null&& !TextUtils.isEmpty(cjpjbzDict.getDictValue())){
                tv_cjpjbz.setText(cjpjbzDict.getDictValue());
            }else {
                tv_cjpjbz.setText("");
            }
            //值类型
            et_input_val.setEnabled(false);
            et_input_val.setBackground(null);
            et_input_remarks.setEnabled(false);
            et_input_remarks.setBackground(null);
            rb_fh.setClickable(false);
            rb_bfh.setClickable(false);
        }
        if(this.data!=null){
            if(smParticulars!=null)tv_project.setText(smParticulars.getPname());
            tv_title.setText(this.data.getDictName()+":");
            et_input_val.setText(this.data.getVal());
            if("1".equals(this.data.getCjjg())){
                cjjgsffh = "1";
                rb_fh.setChecked(true);
            }else {
                cjjgsffh = "0";
                rb_bfh.setChecked(true);
            }
            if("1".equals(this.data.getCjjgtz())){
                cjjgsffhtz = "1";
                rb_fhtz.setChecked(true);
            }else {
                cjjgsffhtz = "0";
                rb_bfhtz.setChecked(true);
            }
            et_input_remarks.setText(this.data.getRemarks());//
            tv_location.setText(this.data.getAddress());
        }
        List<CaFile> images = null;
        List<CaFile> videos = null;
        if(files!=null){
            images = Stream.of(files).
                    filter(i -> i.getFileType().equals(CaFile.type_image))
                    .map(item->{
                        CaFile caFile = JSON.parseObject(JSON.toJSONString(item),CaFile.class);
                        caFile.setFileId(item.getId());
                        return caFile;
                    }).toList();

            videos = Stream.of(files).
                    filter(i -> i.getFileType().equals(CaFile.type_video)).map(item->{
                        AcFileTemp acFileTemp = JSON.parseObject(JSON.toJSONString(item),AcFileTemp.class);
                        CaFile caFile = JSON.parseObject(JSON.toJSONString(acFileTemp),CaFile.class);
                        if(item.getThumb()!=null){
                            CjFile thumb = JSON.parseObject(item.getThumb(),CjFile.class);
                            caFile.setThumb(thumb);
                        }
                        return caFile;
                    }).toList();
        }
        if(images==null&&this.data!=null){
            images = taskGccjFileDao.findByDataIdAndType(this.data.getId(),CaFile.type_image);
        }
        if(videos==null&&this.data!=null){
            videos = taskGccjFileDao.findByDataIdAndType(this.data.getId(),CaFile.type_video);
        }
        take_image.loadDataView(activity,
                images,CaFile.type_image);
        take_video.loadDataView(activity,
                videos,CaFile.type_video);

    }

    public CaDict getDictByCode(String code){
        return Stream.of(dictsOfForm).filter(i->code.equals(i.getDictCode())).findFirstOrElse(null);
    }

    public void setTakeImageViewListener(GccjTakeImageView.TakeImageViewListener takeImageViewListener) {
        this.take_image.setHcCreateViewListener(takeImageViewListener);
    }

    public void addPhotoData(CaFile imageBean) {
        take_image.addData(imageBean);
    }

    public void addVideoData(CaFile imageBean) {
        take_image.addData(imageBean);
    }

    public void addAudioData(CaFile imageBean) {
        take_image.addData(imageBean);
    }
}
